package com.brush.brushbasicapi.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.brush.brushcommon.common.mq.ExchangeConstants.*;

/**
 * @author cws
 * mq消费者业务错误推送至该交换机及队列
 */
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "spring.rabbitmq.listener.simple.retry", name = "enabled", havingValue = "true")
public class ErrorConfiguration {


    /**
     * @author cws
     * @param
     * @return DirectExchange
     * @date 2024/8/2 22:10
     * @description 错误交换机
     */

    @Bean
   public DirectExchange errorExchange(){
        return new DirectExchange(EXCHANGE_DELETE_BASIC_ERROR);
    }


    /**
     * @author cws
     * @param
     * @return Queue
     * @date 2024/8/2 22:10
     * @description 错误队列
     */
    @Bean
    public Queue errorQueue(){
        return new Queue(QUEUE_DELETE_BASIC_ERROR);
    }


    /**
     * @author cws
     * @param
     * @return Binding
     * @date 2024/8/2 22:10
     * @description 错误交换机与队列的绑定
     */
    @Bean
    public Binding errorBinding(){
        return BindingBuilder.bind(errorQueue()).to(errorExchange()).with(ROUTING_KEY_DELETE_BASIC_ERROR);
    }

    /**
     * @author cws
     * @param
     * @return MessageRecoverer
     * @date 2024/8/2 22:10
     * @description 消息重试
     */
    @Bean
    public MessageRecoverer messageRecoverer(RabbitTemplate rabbitTemplate){
        log.info("消息重试配置成功");
        return new RepublishMessageRecoverer(rabbitTemplate, EXCHANGE_DELETE_BASIC_ERROR, ROUTING_KEY_DELETE_BASIC_ERROR);
    }

}

